﻿package 
{
	import flash.display.*;
	import flash.events.*;
	
	public class Gauge extends Sprite
	{
		private var backRect:Shape;
		private var foreRect:Shape;
		private var rectWidth:int;
		private var rectHeight:int;
		private const FULLVALUE:int = 100;
		private const DEFAULTVALUE:int = 20;
		private const ELLIPSWIDTH:int = 1;
	
		public function Gauge(type:String,Width:int,Height:int)
		{
			rectWidth = Width;
			rectHeight = Height;
			
			backRect = new Shape();
			backRect.graphics.beginFill(0xffffff);
			backRect.graphics.drawRoundRect(0,0,rectWidth,rectHeight,rectWidth*ELLIPSWIDTH);
			backRect.graphics.endFill();
			this.addChild(backRect);
			
			switch (type)
			{
				case "Social":
					foreRect = new Shape();
					foreRect.graphics.beginFill(0xff0000);
					foreRect.graphics.drawRoundRect(0,rectHeight-DEFAULTVALUE,rectWidth,DEFAULTVALUE,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
				case "Happiness":
					foreRect = new Shape();
					foreRect.graphics.beginFill(0x00ff00);
					foreRect.graphics.drawRoundRect(0,rectHeight-DEFAULTVALUE,rectWidth,DEFAULTVALUE,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
				case "Tidiness":
					foreRect = new Shape();
					foreRect.graphics.beginFill(0x0000ff);
					foreRect.graphics.drawRoundRect(0,rectHeight-DEFAULTVALUE,rectWidth,DEFAULTVALUE,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
			}
		}
		
		public function onValueChange(Value:int,type:String)
		{
			var Height:int;
			
			if (Value <0 || Value > 100)
			{
				trace("Error: Gauge Value Should Between 1 and 100");
				return;
			}
			
			Height = (Value/100)*rectHeight;
			
			switch (type)
			{
				case "Social":
					this.removeChild(foreRect);
					foreRect.graphics.beginFill(0xff0000ff);
					foreRect.graphics.drawRoundRect(0,rectHeight-Height,rectWidth,Height,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
				case "Happiness":
					this.removeChild(foreRect);
					foreRect.graphics.beginFill(0x00ff00ff);
					foreRect.graphics.drawRoundRect(0,rectHeight-Height,rectWidth,Height,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
				case "Tidiness":
					this.removeChild(foreRect);
					foreRect.graphics.beginFill(0x0000ffff);
					foreRect.graphics.drawRoundRect(0,rectHeight-Height,rectWidth,Height,rectWidth*ELLIPSWIDTH);
					foreRect.graphics.endFill();
					this.addChild(foreRect);
					break;
			}
		}
		
	}
}